package com.ztgf.sys.api.cms;


import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.sys.api.common.annotion.SysLogs;
import com.ztgf.sys.models.bo.SysRoleBO;
import com.ztgf.sys.models.bo.SysRoleSaveBO;
import com.ztgf.sys.models.dto.SysRoleDTO;
import com.ztgf.sys.models.service.ISysRoleMenuRelationService;
import com.ztgf.sys.models.service.ISysRoleService;
import com.ztgf.sys.models.vo.SysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色表  前端控制器
 * </p>
 *
 * @author YUE123
 * @since 2020-05-26
 */
@RestController
@RequestMapping("/cms/sysRole")
@Slf4j
@Api(value = "SysRoleController",tags = "角色表接口")
public class SysRoleController {

    @DubboReference
    private ISysRoleService iSysRoleService;

    @DubboReference
    private ISysRoleMenuRelationService sysRoleMenuRelationService;

    @ApiOperation(value = "获取角色信息",notes = "获取角色信息")
    @PostMapping(value = "/get")
    public Result get(Long id) {
        if(null == id){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SysRoleDTO sysRoleDTO = iSysRoleService.selectById(id);
        return new Result<>(sysRoleDTO);
    }

    @SysLogs(value = "添加角色")
    @ApiOperation(value = "添加角色表",notes = "添加角色表")
    @PostMapping(value = "/insert")
    public Result insert(SysRoleSaveBO sysRoleSaveBO) {
        try {
            iSysRoleService.add(sysRoleSaveBO);
        } catch (Exception e) {
            return new Result<>(CodeEnum.FAIL_100007);
        }
        return new Result<>();
    }

    @SysLogs(value = "修改角色")
    @ApiOperation(value = "修改角色表",notes = "修改角色表")
    @PostMapping("/update")
    public Result update(SysRoleSaveBO sysRoleSaveBO){
        if(null == sysRoleSaveBO.getId()){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SysRoleDTO sysRoleDTO = iSysRoleService.selectById(sysRoleSaveBO.getId());
        sysRoleDTO.setName(sysRoleSaveBO.getName());
        boolean flag = iSysRoleService.updateById(sysRoleDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @SysLogs(value = "删除角色")
    @ApiOperation(value = "删除角色表",notes = "删除角色表")
    @ApiImplicitParam(name = "id",value = "角色表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = iSysRoleService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "角色表列表",notes = "角色表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysRoleVO>> queryPageList(SysRoleBO sysRoleBO){
        Page<SysRoleVO> page = iSysRoleService.selectPageRoleByWebType(sysRoleBO);
        return new Result<>(page);
    }
}